home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
basic
/
chaosexe.zip
/
XEXPFFT.TRU
< prev
next >
Wrap
Text File
|
1990-03-15
|
6KB
|
197 lines
1000 !PROGRAM XEXPFFT.TRU
1010 LIBRARY "SGLIB.TRC"
1011 CLEAR
1012 PRINT" ***FFT OF SUPERPOSED SINE WAVES***"
1013 PRINT
1014 PRINT"THIS PROGRAM TAKES THE FOURIER TRANSFORM OF A GROUP OF SINE"
1015 PRINT"WAVES WHOSE AMPLITUDES AND FREQUENCIES ARE INPUTS. BOTH THE"
1016 PRINT"TIME SERIES AND THE TRANSFORM ARE GRAPHED. IF A BLINKING CURSOR"
1017 PRINT" APPEARS PRESS ANY KEY TO CONTINUE. THE HANNING OPTION SMOOTHS "
1018 PRINT" THE ABRUPT EFFECT OF THE WINDOW "
1019 PRINT" AND SUPPRESSES SPURIOUS COMPONENTS."
1030 DIM thetadata(5000),thetadotdata(5000),xreal(0 to 5000),ximag(0 to 10000)
1040 DIM tpoint(0 to 5000),power(2048),frequency(2048),FREQ(10),AMPL(10)
1050 DECLARE DEF bitr
1060 INPUT prompt"Max frequency : ":maxfreq
1070 INPUT Prompt"Input min.time:":tmin
1080 INPUT prompt"No. of FFT points(..256,512,1024,2048,4096) : ":number
1090 LET ps=1
1100 LET del=.5/maxfreq
1110 LET tmax=number*del+tmin
1120 LET n=number
1130
1140 LET count=0
1150 LET p=1
1160 !DEVELOP PERIODIC TIME SERIES DATA
1170 INPUT PROMPT "HOW MANY FREQUENCY COMPONENTS?":NUMBFREQ
1180 FOR NF = 1 TO NUMBFREQ
1190 INPUT PROMPT" STATE FREQUENCY :":FREQ(NF)
1200 INPUT PROMPT" COMPONENT AMPLITUDE:":AMPL(NF)
1210 NEXT NF
1220 FOR P = 1 TO N
1230 LET TOTAL = 0
1240 FOR NF = 1 TO NUMBFREQ
1250 LET TOTAL = TOTAL + AMPL(NF)*SIN(2*PI*FREQ(NF)*(TMIN+P*DEL))
1260 NEXT NF
1270 LET THETADOTDATA(P)=TOTAL
1280 NEXT P
1290 !
1300 !
1310 !PREPARATION OF THE FFT DATA
1320 CLEAR
1330 INPUT prompt" HANNING OPTION Y/N? ": hanning$
1340 LET tgamma=log2(n)
1350 IF abs(int(tgamma)-tgamma)=0 then
1360 LET gamma=tgamma
1370 GOTO 1400
1380 END IF
1390 LET gamma=int(tgamma)+1
1400 PRINT "gamma= ";gamma
1410 LET newn=2^gamma
1420 LET nu=gamma
1430 FOR i=n+1 to newn
1440 LET xreal(i)=0
1450 NEXT i
1460 LET n=newn
1470 PRINT"n=";n
1480 CLEAR
1490 IF ps=1 then LET title$="WAVE DISPLACEMENT"
1500 CALL settext("TIME SERIES","TIME",title$)
1510 CALL setxscale(tmin,tmax)
1520 FOR k=0 to n-1
1530
1540 IF ps = 1 then
1550 LET xreal(k)=thetadotdata(k+1)
1560 ELSE IF ps = 2 then
1570 LET xreal(k)=thetadata(k+1)
1580 END IF
1590 IF hanning$="y" then LET xreal(k) = xreal(k)*(.5-.5*cos(2*pi*k/(n-1)))
1600 LET ximag(k)=0
1610 LET tpoint(k)=tmin+k*del
1620 NEXT k
1630 CALL SETAXES(0)
1640 CALL setgraphtype("")
1650 CALL datagraph(tpoint,xreal,1,0,"white")
1660 GET KEY keyvariable
1670 FOR i= 1 to 100
1680 NEXT i
1690
1700 !FFT ALGORITHM
1710 CLEAR
1720 PRINT "Calculating FFT"
1730 LET n2=n/2
1740 LET nu1=nu-1
1750 LET k=0
1760 FOR l=1 to nu
1770 DO while k<(n-1)
1780 FOR i=1 to n2
1790 LET argument=k/2^nu1
1800 LET garbage=int(argument)
1810 LET p=bitr(garbage,nu)
1820 LET arg =2*pi*p/n
1830 LET c=cos(arg)
1840 LET s=sin(arg)
1850 LET k1=k+1
1860 LET k1n2=k1+n2
1870 LET treal=xreal(k1n2)*c+ximag(k1n2)*s
1880 LET timag=ximag(k1n2)*c-xreal(k1n2)*s
1890 LET xreal(k1n2)=xreal(k1)-treal
1900 LET ximag(k1n2)=ximag(k1)-timag
1910 LET xreal(k1)=xreal(k1)+treal
1920 LET ximag(k1)=ximag(k1)+timag
1930 LET k=k+1
1940 NEXT i
1950 LET k=k+n2
1960 LOOP
1970 LET k=0
1980 LET nu1=nu1-1
1990 LET n2=int(n2/2)
2000 NEXT l
2010
2020 FOR k=1 to n
2030 LET i=bitr(k-1,nu)+1
2040 IF i<=k then GOTO 2110
2050 LET treal=xreal(k)
2060 LET timag=ximag(k)
2070 LET xreal(k)=xreal(i)
2080 LET ximag(k)=ximag(i)
2090 LET xreal(i)=treal
2100 LET ximag(i)=timag
2110 NEXT k
2120
2130 !GRAPHING THE FFT
2140 CLEAR
2150 INPUT prompt"Plot 1)power spectrum, or 2)log power spectrum: ":pps
2160 INPUT prompt"Frequency variable - 1)linear, or 2)log: ":freqvar
2170 LET maxfreq=.5/del
2180 LET minfreq=1/(number*del)
2190
2200 CLEAR
2210 !Y-AXIS
2220 IF pps = 1 then
2230 LET TITLE$="POWER SPECTRUM"
2240 LET YAXIS$="POWER"
2250 ELSE
2260 LET TITLE$="LOG POWER SPECTRUM"
2270 LET YAXIS$="LOG POWER"
2280 END IF
2290 !X-AXIS
2300 IF freqvar=2 then
2310 LET XAXIS$="LOG FREQUENCY"
2320 ELSE
2330 LET XAXIS$="FREQUENCY"
2340 END IF
2350
2360 !DRAW AXES
2370 CLEAR
2380 CALL setxscale(minfreq,maxfreq)
2390 CALL setyscale(1e-6,.99)
2400 CALL SETTEXT(TITLE$,XAXIS$,YAXIS$)
2410 CALL RESERVELEGEND
2420
2430 !PLOT POINTS
2440 FOR i=1 to n/2
2450 LET frequency(i)=i/(n*del)
2460 LET power(i)=((((xreal(i))^2+(ximag(i))^2))^.5)/n
2480
2490 NEXT i
2500 !PLOT TEXT
2510 CALL setaxes(0)
2520 IF pps=1 then
2530 IF freqvar=1 then CALL setgraphtype("xy")
2540 IF freqvar=2 then CALL setgraphtype("logx")
2550 END IF
2560 IF pps=2 then
2570 IF freqvar=1 then CALL setgraphtype("logy")
2580 IF freqvar=2 then CALL setgraphtype("logxy")
2590 END IF
2595 IF NUMBER =4096 THEN
2596 LET SYMBOL=1
2597 ELSE
2598 LET SYMBOL=0
2599 END IF
2600 CALL datagraph(frequency,power,1,SYMBOL,"white")
2610 CALL ADDLEGEND("N="&STR$(N)&" MAX FREQ="&STR$(MAXFREQ)&" DEL F="&STR$(MINFREQ),0,1,"WHITE")
2620 CALL drawlegend
2630 IF hanning$="y" then
2640 CALL ADDLEGEND(" HANNING",0,1,"WHITE")
2650 END IF
2660 GET KEY keyvariable
2670 INPUT PROMPT "Another with Hanning? y/n: ":hann$
2680 IF hann$= "y" THEN GOTO 1320
2690 INPUT PROMPT "Different presentation of same FFT? (y/n): ":diffplot$
2700 IF diffplot$ ="y" THEN GOTO 2140
2710 END
2720 !
2730 !BIT REVERSER FUNCTION
2740 DEF bitr(j,nu)
2750 LET j1=j
2760 LET ibitr=0
2770 FOR i=1 to nu
2780 LET j2 = int(j1/2)
2790 LET ibitr=ibitr*2+(j1-2*j2)
2800 LET j1=j2
2810 NEXT i
2820 LET bitr=ibitr
2830 END DEF